<?php
class FieldController extends Zend_Controller_Action
{
    public function init ()
    {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity()){
            $acl = new My_Acl();
            if(!$acl->isAllowed(Zend_Auth::getInstance()->getIdentity(), $this->_request)){
                $this->_redirect('/default/error/accessdenied');
            }
        }else{
            return $this->_helper->redirector('index', 'auth');
        }
    }
    public function indexAction ()
    {
        $model_field = new Default_Model_Field();
        $this->view->fields = $model_field->getAllData();
    }
    public function deleteAction ()
    {
        if ($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $fieldId = $this->getRequest()->getPost('fieldId');
                $model_field = new Default_Model_Field();
                $model_field->deleteData($fieldId);
                $model_equipmentfield = new Default_Model_EquipmentField();
                $model_equipmentfield->deleteFieldId($fieldId);
            }
            $this->_helper->redirector('index','field');
        }else{
            $id = $this->_getParam('id', 0);
            $model_field = new Default_Model_Field();
            $this->view->field = $model_field->getDataByID($id);
        }
    }
    public function addAction ()
    {
        $form = new Default_Form_Field();
        $form->submit->setLabel('Thêm mới!');
        $this->view->form = $form;
        
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)){
                $fieldName = $form->getValue('fieldName');
                $type = $form->getValue('type');
                
                $model_field = new Default_Model_Field();
                $field_check = $model_field->getDataByName($fieldName);
                
                if(!isset($field_check)){
                    $model_field->addData($fieldName, $type);
                }
                $this->_helper->redirector('index','field');
            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction()
    {
		$form = new Default_Form_Field();
		$form->submit->setLabel( 'Lưu' );
		$this->view->form = $form;
	
		if ($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)){
				$fieldId = (int) $form->getValue( 'fieldId' );
				$fieldName = $form->getValue( 'fieldName' );
				$type = $form->getValue( 'type' );

				$model_field = new Default_Model_Field();
                $field_check = $model_field->getDataByName($fieldName);
                if(!$field_check){
				    $model_field->updateData($fieldId, $fieldName, $type);
                }
				$this->_helper->redirector('index','field');
			}else{
				$form->populate($formData);
			}
		}else{
			$id = $this->_getParam('id', 0);
			if($id > 0){
				$model_field = new Default_Model_Field();
                $field = $model_field->getDataByID($id);
				$form->populate($field);
			}
		}
    }
}





